package com.airnews.ticket.net;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import org.kobjects.base64.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class Connection {
	private static final String NAMESPACE = "http://impl.service.qhcontent.sunrise.com/";
	private static final String TAG="Connection";
	// WebService
	private static final String SYSTEM_END_POINT ="http://121.33.246.236:8080/qhwxcsBE/ws/systemOperation";
	private static final String BUSINESS_END_POINT ="http://121.33.246.236:8080/qhwxcsBE/ws/businessOperation";
	
	private SoapObject rpc;
	private SoapSerializationEnvelope envelope;
	private static final HttpTransportSE SYSTEM_HT=new HttpTransportSE(SYSTEM_END_POINT);
	private static final HttpTransportSE BUSINESS_HT=new HttpTransportSE(BUSINESS_END_POINT);
	private HttpTransportSE ht;
	
	private Object[] params;
	
	private static Connection connection;
	
	/**
	 * constructor
	 */
	private Connection(){
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;			
	}
	
	public void setHt(HttpTransportSE ht) {
		this.ht = ht;
		ht.debug = true;
	}
   
	public void setHt(boolean isSystemOp) {
	if(isSystemOp){
		setHt(SYSTEM_HT);
	}
	else{
		setHt(BUSINESS_HT);
	}
	}
	
	public static Connection getInstance(){
		if(connection == null){
			connection = new Connection();
		}
		return connection;
	}
	
	/**
	 * 
	 * @param methodName
	 * 			the name of method which you want to invoke
	 * @param params 
	 * 			the method's params
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public String invoke(boolean isSystemOp,String methodName,Object...params) throws IOException, XmlPullParserException{
		setHt(isSystemOp);
		this.params = params;
		rpc = new SoapObject(NAMESPACE, methodName);
		for (Object object : params) {
			rpc.addProperty("star"+object.toString(), object.toString());
		}
		envelope.bodyOut = rpc;
		envelope.setOutputSoapObject(rpc);
		String soapAction=NAMESPACE+"/"+methodName;
        ht.call(soapAction, envelope);	
		Log.i(TAG, "send:"+ht.requestDump);
		Log.i(TAG, "recieve:"+ht.responseDump);
//		SoapObject soapObject = (SoapObject) envelope.getResponse();  
//		SoapPrimitive sp = 	(SoapPrimitive)soapObject.getProperty(0);	
//		return sp.toString();
		
		SoapObject result = (SoapObject)envelope.bodyIn;
		
		byte[] ops = Base64.decode(result.getProperty(0).toString());
		
		ByteArrayInputStream bin = new ByteArrayInputStream(ops);
		DataInputStream dis = new DataInputStream(bin);
		String newName;
		try {
			newName = dis.readUTF();
			System.out.println("readUTF==="+newName);
			Log.i(TAG, "newName="+newName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
		String str = new String(ops);
		Log.i(TAG, str.trim());
		return str;
	}
}
